#ifndef TC_NEWHANDLER_HH
#define TC_NEWHANDLER_HH

/** \file NewHandler.hh 
 * Declarations for the class NewHandler.
 *
 * (C) Copyright Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: NewHandler.hh 111 2006-06-19 03:45:40Z semihc $
 *
 *
 */

#include <new>
#include "Global.hh"

namespace TC {


  class NewHandler
  {
  public:

    //! Default constructor
    NewHandler();
    
    //! Destructor
    ~NewHandler();

    //! Allocate reserve memory of desired size. If the size 
    //! is not available, halve it until it succeeds.
    int allocateReserve(int desiredSize);

    //! Release the reserve memory.
    void releaseReserve();

    //! Get the reserve memory size
    int getReserveSize() const { return m_rsize; }
    
    //! Install a new default new-handler. Return the old one.
    std::new_handler installHandler(std::new_handler hlr);

    //! Uninstall default new-handler, re-install old one as default.
    void uninstallHandler();

    //! Access to the singleton instance
    static NewHandler* Instance();
    
    //! Deallocate the singleton instance
    static void DeleteInstance();

  private:

    //! Size of the reserve memory.
    int m_rsize;

    //! Address of the reserve memory.
    char* m_reserve;
    
    //! Previous handler.
    std::new_handler m_oldhandler;
    
    //! The only instance.
    static NewHandler* s_Instance;
     
  }; // end class


  //! Substitute for the default new handler
  void Global_new_handler();


} // end namespace




#endif /* Include guard */
